package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.AdminRoleService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created on 2023/6/27
 */
@Api(tags = "角色分页处理")
@RestController
@RequestMapping("/admin/acl/role")
@CrossOrigin//解决前端跨域问题
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private AdminRoleService adminRoleService;

    /**
     * 根据角色名称查询分页信息
     */
    @ApiOperation("根据角色名称查询分页信息")
    @GetMapping("{current}/{limit}")
    public Result pageList(@PathVariable int current,
                           @PathVariable int limit,
                           RoleQueryVo roleQueryVo){
        /**
         * 处理分页参数
         * current 当前页
         * limit
         */
        Page<Role> pageParam = new Page<>(current, limit);

        //执行分页查询
        IPage<Role> pageModel = roleService.selectPageVo(pageParam, roleQueryVo);

        return Result.ok(pageModel);
    }

    /**
     * 根据角色id查询角色信息
     */
    @ApiOperation("根据角色id查询角色信息")
    @GetMapping("get/{id}")
    public Result getById(@PathVariable Long id){
        Role role = roleService.getById(id);
        return Result.ok(role);
    }

    /**
     * 添加角色
     */
    @ApiOperation("添加角色")
    @PostMapping("save")
    public Result save(@RequestBody Role role){
        boolean is_Success = roleService.save(role);
        if(is_Success){
            return Result.ok(null);
        }
        return Result.fail(null);
    }

    /**
     * 更新角色
     */
    @ApiOperation("更新角色")
    @PutMapping("update")
    public Result updateById(@RequestBody Role role){
        boolean is_Success = roleService.updateById(role);
        if(is_Success){
         return Result.ok(null);
        }
        return Result.ok(null);
    }

    /**
     * 删除一个角色
     */
    @ApiOperation("删除一个角色")
    @DeleteMapping("remove/{id}")
    public Result delete(@PathVariable Long id){
        boolean is_Success = roleService.removeById(id);
        if(is_Success){
            return Result.ok(null);
        }
        return Result.ok(null);
    }

    /**
     * 批量删除角色
     */
    @ApiOperation("批量删除角色")
    @DeleteMapping("batchRemove")
    public Result removeRoles(@RequestBody List<Long> ids){
        boolean is_Success = roleService.removeByIds(ids);
        if(is_Success){
            return Result.ok(null);
        }
        return Result.ok(null);
    }

}
